{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "40a23969-b4af-466f-8745-74991605e683",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1b9740f-24ff-4f38-96cd-12c73bf5e19b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "img1 type is <class 'PIL.JpegImagePlugin.JpegImageFile'>\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e280a179-8935-43a1-87b7-d56ea4bfbeb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "img1 type is <class 'PIL.JpegImagePlugin.JpegImageFile'>\n",
      "img2 type is <class 'PIL.BmpImagePlugin.BmpImageFile'>\n",
      "img3 type is <class 'PIL.JpegImagePlugin.JpegImageFile'>\n"
     ]
    }
   ],
   "source": [
    "img1 = Image.open(r\"D:\\desktop\\dog.jpg\")\n",
    "print(f\"img1 type is {type(img1)}\")\n",
    "img2 = Image.open(r\"D:\\desktop\\dog.bmp\")\n",
    "print(f\"img2 type is {type(img2)}\")\n",
    "img3 = Image.open(r\"D:/desktop/gray.jpg\")\n",
    "print(f\"img3 type is {type(img3)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8a703f2b-2647-416a-9ea2-0dcbaed11991",
   "metadata": {},
   "outputs": [],
   "source": [
    "img1.show()\n",
    "img2.show()\n",
    "img3.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "008ebaa7-5957-4c4a-9c35-349e3bf0619f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RGB\n",
      "RGB\n",
      "L\n"
     ]
    }
   ],
   "source": [
    "print(img1.mode)\n",
    "print(img2.mode)\n",
    "print(img3.mode)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f967eec1-0538-473e-99cf-ed7f6b1c6ade",
   "metadata": {},
   "source": [
    "**将图片类型转化为数组格式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1e481651-f8f6-4186-abe9-e80860a0b105",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "img1_np shape is (1024, 742, 3)\n",
      "img2_np shape is (1080, 1728, 3)\n",
      "img3_np shape is (2460, 1967)\n"
     ]
    }
   ],
   "source": [
    "img1_np = np.array(img1) #将图片转化为数组\n",
    "print(f\"img1_np shape is {img1_np.shape}\") #数组的维度\n",
    "img2_np = np.array(img2) #将图片转化为数组\n",
    "print(f\"img2_np shape is {img2_np.shape}\") #数组的维度\n",
    "img3_np = np.array(img3) #将图片转化为数组\n",
    "print(f\"img3_np shape is {img3_np.shape}\") #数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3146db95-ed20-4bd1-b119-afcc6131d368",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将3通道图存储为单通道图片\n",
    "# img_grey = cv2.cvtColor(img3_np , cv2.COLOR_RGB2GRAY)\n",
    "# cv2.imwrite(r\"D:\\desktop\\gray.jpg\", img_grey)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb57185c-17aa-4b64-844d-24d88fbe610f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rgb = 237,242,245\n",
    "img1_np[221][357][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61c14531-3526-4420-bfd2-ae217cc905ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img1_np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93a6672f-fc08-4a6f-924a-9ec10167467c",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img2_np)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fe33f61-ebcd-42aa-be89-831fe2540a28",
   "metadata": {},
   "source": [
    "**将图片向量化**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "348c079e-39b7-4abc-9e2e-98df6c2143f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# img2_np_transposed = img2_np.transpose((1, 0, 2))\n",
    "# print(img2_np_transposed.shape)\n",
    "# plt.imshow(img2_np_transposed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b67c2e9b-e831-4026-97bd-42ed895e4643",
   "metadata": {},
   "outputs": [],
   "source": [
    "img1_reshape = img1_np.reshape(-1,1)\n",
    "print(img1_reshape.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f33e0f-240a-48a8-a5b5-954c837923e7",
   "metadata": {},
   "source": [
    "[插值算法介绍](https://blog.csdn.net/u012294613/article/details/140974770)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "64f33a5d-3710-40f6-af66-6c164fc975a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#默认使用的是双线性插值,最简单的插值法,提供的插值法都是线性插值法\n",
    "img1.show()\n",
    "img1_resize=img1.resize((500,500),resample=Image.BILINEAR)\n",
    "img1_resize.show()\n",
    "# img1_resize=img1.resize((500,500),Image.BILINEAR) #双线性插值\n",
    "# img1_resize=img1.resize((500,500),Image.BICUBIC) #双三次插值\n",
    "# img1_resize=img1.resize((500,500),Image.LANCZOS) #兰佐斯插值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4d8ec75-0ea0-4092-bad7-a7193136cf8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(img1_resize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eed0b892-22a5-4496-8b08-ad7fb8c12306",
   "metadata": {},
   "outputs": [],
   "source": [
    "gray_values = np.arange(1, 101, 1).reshape(10, 10).astype(np.uint8) \n",
    "print(gray_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35b7fc25-df3b-4d02-8bd9-488b80e22dbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "img3 = Image.fromarray(gray_values.astype(np.uint8),'L')\n",
    "print(img3.mode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102c6ef1-70c9-40b9-8d44-43526d71431a",
   "metadata": {},
   "outputs": [],
   "source": [
    "img3_resize=img3.resize((5,5),resample=Image.BILINEAR)\n",
    "print(np.array(img3_resize))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a080a4c-e3a0-4b62-a886-204b48dcd4fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv_resized = cv2.resize(gray_values, (5, 5), interpolation=cv2.INTER_LINEAR)\n",
    "print(cv_resized)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b11bc8ca-1cbc-4ac2-929f-a5edc7fb0bf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv_img1 = cv2.imread(img_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85f8e7e-2763-4956-bc1b-458ebb21e51b",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv2.imshow('img',cv_img1)\n",
    "cv2.waitKey(0)  # 等待按键\n",
    "cv2.destroyAllWindows()  # 关闭所有窗口"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
